<?
function drizzo_event_save() {
	global $drizzo_events;
	$_POST = drizzo_db_wrap($_POST);
	extract($_POST);
	$tags = drizzo_parse_the_tags($tags);
	$start = gmmktime($start_date_hour,$start_date_min,00,$start_date_month,$start_date_day,$start_date_year);
	$end = gmmktime($end_date_hour,$end_date_min,59,$end_date_month,$end_date_day,$end_date_year);
	if(strlen(trim($permalink_name)) > 0) {
		$permalink_name = preg_replace('[^A-Za-z_-0-9]','-',trim($permalink_name));
	} else {
		$permalink_name = substr(strtolower(preg_replace('/[^A-Za-z_0-9-]/','-',trim($what))),0,100);
	}
	$edited_date = time();
	$edited_by = session_user_id;
	if($_GET['action'] == 'edit') {
		$action = "UPDATE";
		$args = " WHERE `ID`='$ID'";
		$the_event = drizzo_get_event($ID);
		if(!drizzo_user_is_allowed('gallery-publish')) {
			$status = 'draft';
		}
	} else {
		$action = "INSERT INTO";
		$added_by = session_user_id;
	}
	if($draft) {
		$status = 'draft';
	} elseif($publish) {
		$status = 'publish';
	}
	$sql = "$action `$drizzo_events` SET `start`='$start',`end`='$end',`timezone_offset`='$timezone_offset',`what`='$what',`where`='$where',`who`='$who',`notes`='$notes',`tags`='$tags',`added_by`='$added_by',`edited_by`='$edited_by',`edited_date`='$edited_date',`status`='$status',`permalink_name`='$permalink_name'$args";
	@mysql_query($sql);
	if(!mysql_errno()) {
		return true;
	} else {
		return false;
	}
}

function drizzo_event_delete() {
	global $drizzo_events;
	if(drizzo_event_permissions_check($_GET['id'])) {
		@mysql_query("DELETE FROM `$drizzo_events` WHERE `ID`='$_GET[id]'");
		if(!mysql_errno()) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function drizzo_event_permissions_check($id = null) {
	global $the_event;
	if($id != null) {
		$the_event = drizzo_get_event($id);
	}
	if(drizzo_user_is_allowed('events-publish') || $the_event['author'] == session_user_id) {
		return true;
	} else {
		return false;
	}
}

function drizzo_get_event($ID) {
	global $drizzo_events;
	$event_lookup = mysql_query("SELECT * FROM `$drizzo_events` WHERE `ID`='$ID'");
	if(!mysql_errno() && $event = @mysql_fetch_assoc($event_lookup)) {
		$event							=	drizzo_db_unwrap($event);
		$event['notes']					=	drizzo_texturize($event['notes']);
		$event['start_raw']				=	$event['start'];
		$event['start']					=	gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['start']));
		$event['end_raw']				=	$event['end'];
		$event['end']					=	gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['end']));
		$event['edited_date_raw']		=	$event['edited_date'];
		$event['edited_date']			=	gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['edited_date']));
		$event['author']				=	$event['added_by'];
		$event['author_display_name']	=	drizzo_user_info($event['added_by'],'display_name');
		$event['editor']				=	$event['edited_by'];
		$event['editor_display_name']	=	drizzo_user_info($event['edited_by'],'display_name');
		return $event;
	} else {
		return false;
	}
}

function drizzo_get_events($start = null, $end = null, $limit = null, $offset = null, $type = "'publish'") {
	global $drizzo_events;
	if(substr_count($type,',') > 0) {
		$types = explode(',',$type);
		unset($type);
		foreach($types as $a_type) {
			if(substr($a_type,0,1) != "'") {
				$a_type = "'$a_type";
			}
			if(substr($a_type,-1,1) != "'") {
				$a_type = "$a_type'";
			}
			$type .= $a_type;
			if($i < (count($types) - 1)) {
				$type .= ",";
			}
			$i++;
		} 
	} else {
		if($type[0] != "'") {
			$type = "'$type";
		}
		if($type[strlen($type) - 1] != "'") {
			$type = "$type'";
		}
	}
	if($limit != null && $offset != null) {
		$args = " $offset,$limit";
	} elseif($limit != null && $offset == null) {
		$args = " 0,$limit";
	}
	if($start != null && $end == null) {
		$args = "WHERE `start`>='$start' AND `status` IN ($type) ORDER BY `start` ASC";
	} elseif($start == null && $end != null) {
		$args = "WHERE `end`<='$end' AND `status` IN ($type) ORDER BY `start` DESC";
	} elseif($start != null && $end != null) {
		$args = "WHERE `start`>='$start' AND `end`<='$end' AND `status` IN ($type) ORDER BY `start` DESC";
	} else {
		$args = "WHERE `status` IN ($type) ORDER BY `start` DESC";
	}
	$events_lookup = @mysql_query("SELECT * FROM `$drizzo_events` $args");
	if(!mysql_errno() && mysql_num_rows($events_lookup) > 0) {
		while($event = @mysql_fetch_assoc($events_lookup)) {
			$event							=	drizzo_db_unwrap($event);
			$event['notes']					=	drizzo_texturize($event['notes']);
			$event['start_raw']				=	$event['start'];
			$event['start']					=	gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['start']));
			$event['end_raw']				=	$event['end'];
			$event['end']					=	gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['end']));
			$event['edited_date_raw']		=	$event['edited_date'];
			$event['edited_date']			=	gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['edited_date']));
			$event['author']				=	$event['added_by'];
			$event['author_display_name']	=	drizzo_user_info($event['added_by'],'display_name');
			$event['editor']				=	$event['edited_by'];
			$event['editor_display_name']	=	drizzo_user_info($event['edited_by'],'display_name');
			$events[]						=	$event;
		}
		return $events;
	} else {
		return false;
	}
}

function drizzo_event_permalink($id = null) {
	if($id != null) {
		$the_event = drizzo_get_event($id);
	} else {
		global $the_event;
	}
	if(drizzo_global_options('use_permalinks') == 1) {
		$permalink = domain . drizzo_global_options('events_basename') . "/" . gmdate('Y/m/d/', drizzo_timezone_offset($the_event['start_raw'])) . $the_event['permalink_name'];
	} else {
		$permalink = domain . "?event=" . $the_event['ID'];
	}
	return $permalink;
}

function drizzo_events_table() {
	$the_events = drizzo_get_events();
			$table	 = 	"<table>\n";
			$table	.=	"\t<tr id=\"first\">\n";
			$table	.=	"\t\t<th class=\"center narrow\">ID</th><th>Title</th><th>Date</th><th>Tags</th><th class=\"center\">Author</th><th class=\"narrow\" colspan=\"2\">Actions</th>\n";
			$table	.=	"\t</tr>\n";
	if($the_events) {
		foreach($the_events as $the_event) {
			$table	.=	"\t<tr>\n";
			$table	.=	"\t<td class=\"narrow center\"><strong>$the_event[ID]</strong></td>";
			if($the_event['status'] == 'publish') {
			$table	.=	"<td><a href=\"" . drizzo_event_permalink($the_event['ID']) . "\" title=\"View $the_event[what]\">$the_event[what]</a></td>";
			} else {
			$table	.=	"<td><em><a href=\"" . drizzo_event_permalink($the_event['ID']) . "\" title=\"View $the_event[what]\">$the_event[what]</a></em></td>";
			}
			$table	.=	"<td>$the_event[start] - $the_event[end]</td><td>$the_event[tags]</td><td class=\"center\">$the_event[author_nicename]</td>";
			if(drizzo_user_is_allowed('events-publish') || $the_event['author'] == session_user_id) {
			$table	.=	"<td><a href=\"events.php?action=edit&amp;id=$the_event[ID]\" title=\"Edit $the_event[what]\" class=\"edit_link\"><span>Edit</span></a></td><td><a href=\"events.php?action=delete&amp;id=$the_event[ID]\" title=\"Delete $the_event[what]\" class=\"delete_link\"><span>Delete</span></a></td>\n";
			} else {
			$table	.=	"<td></td><td></td>\n";
			}
			$table	.=	"\t</tr>\n";
		}
	} else {
			$table	.=	"\t<tr>\n";
			$table	.=	"\t<td colspan=\"7\"><strong>No Events could be found.</strong></td>\n";
			$table	.=	"\t</tr>\n";
	}
			$table	.=	"</table>\n";
	return $table;
}

function drizzo_events_upcoming($limit = '10', $list_type = "ul", $item_before = "<li class=\"events_list_item\">") {
	$today = gmmktime(0, 0, 0, gmdate('m'), gmdate('d'), gmdate('Y'));
	$the_events = drizzo_get_events($today,null,$limit,0,'publish');
	$list_type = preg_replace('[^uol]','',$list_type);
	if($the_events) {
			$list	=	"<$list_type id=\"events_list\">\n"; 
		foreach($the_events as $the_event) {
			$list	.=	"\t$item_before<a href=\"" . drizzo_event_permalink($the_event['ID']) . "\" title=\"$the_event[what] ($the_event[start] - $the_event[end])\">$the_event[what]<span class=\"events_list_time\"> - $the_event[start]</span></a></li>\n";
		}
			$list	.=	"</$list_type>\n";
		return $list;
	} else {
		return false;
	}
}

function drizzo_events_month($style = 'detailed', $month = null,$year = null) {
	global $drizzo_events;
	if($month == null) {
		$month = gmdate('m');
	}
	if($year == null) {
		$year = gmdate('Y');
	}
	$start			=	gmmktime(0, 0, 0, $month, 1, $year);
	$days_in_month	=	gmdate('t', gmmktime(0,0,0,$month,1,$year));
	$end			=	gmmktime(23,59,59, $month, $days_in_month, $year);
	$events_lookup	=	@mysql_query("SELECT * FROM `$drizzo_events` WHERE `start` >= '$start' AND `start` <= '$end' ORDER BY `start` ASC");
	$start_day		=	gmdate('w',$start);
	$end_day		=	gmdate('w',$end);
	if($days_in_month == 28 && $start_day == 0) {
		$total_days = $days_in_month;
	} else {
		for($i = 0; $i < 6; $i++) {
			if(($days_in_month + $start_day + $i) % 7 == 0) {
				$total_days = $days_in_month + $start_day + $i;
			}
		}
	}
	if(!mysql_errno() && @mysql_num_rows($events_lookup) > 0) {
		while($event = @mysql_fetch_assoc($events_lookup)) {
			$event['start_raw'] = $event['start'];
			$event['start'] = gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['start']));
			$event['end_raw'] = $event['end'];
			$event['end'] = gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['end']));
			$event['edited_date_raw'] = $event['edited_date'];
			$event['edited_date'] = gmdate(drizzo_global_options('time_format'), drizzo_timezone_offset($event['edited_date']));
			$event['author'] = $event['added_by'];
			$event['author_nicename'] = drizzo_user_info($event['added_by'],'nice_name');
			$event['editor'] = $event['edited_by'];
			$event['editor_nicename'] = drizzo_user_info($event['edited_by'],'nice_name');
			$the_events[gmdate('j',drizzo_timezone_offset($event['start_raw']))][] = drizzo_db_unwrap($event);
		}
	}
	$table	 =	"<table id=\"event_month_$style\">\n";
	$table	.=	"\t<tr>\n";
	$table	.=	"\t\t<th colspan=\"7\">" . gmdate('F',$start) . "</th>\n";
	$table	.=	"\t</tr>";	
	for($i = 0; $i < $total_days; $i++) {
		if($day_number == 0) {
			$table	.=	"\t<tr>\n\t\t";
		}
		if(($row_number == 0 && $i < $start_day) || $i >= ($days_in_month +  $start_day) || ($i == 0 && $start_day != 0)) {
			$table	.=	"<td class=\"event_month_empty\"></td>";
		} elseif(($row_number == 0 && $i >= $start_day) || $row_number > 0) {
			if($style == 'simple') {
				if($the_events[$i - $start_day + 1]) {
					$table	.=	"<td><a href=\"" . domain . "events/$year/$month/" . ($i - $start_day + 1) . "\" title=\"View Events\">" . ($i - $start_day + 1) . "</a></td>";
				} else {
					$table	.=	"<td>" . ($i - $start_day + 1) . "</td>";
				}
			} elseif($style == 'detailed') {
				if($the_events[$i - $start_day + 1]) {
				$table	.=	"<td><div class=\"event_month_date\"><a href=\"" . domain . "events/$year/$month/" . ($i - $start_day + 1) . "\" title=\"View Events\">" . ($i - $start_day + 1) . "</a></div><ul>";
					for($x = 0; $x < count($the_events[$i - $start_day + 1]); $x++) {
						$the_event = $the_events[$i - $start_day + 1][$x];
						$table	.=	"<li><a href=\"" . drizzo_event_permalink($the_event['ID']) . "\" title=\"$the_event[what] ($the_event[start] - $the_event[end])\">$the_event[what]</a></li>";
					}
					$table	.=	"</ul></td>";
				} else {
					$table	.=	"<td><div class=\"event_month_date\">" . ($i - $start_day + 1) . "</div></td>";
				}
			}
		}
		if($day_number == 6) {
			$table	.=	"\n\t</tr>\n";
			$row_number++;
			$day_number = 0;
		} else {
			$day_number++;
		}
	}
	$table	.= "</table>";
	return $table;
}

function drizzo_timezones_dropdown() {
	global $the_event;
	if($the_event) {
		$selected = $the_event['timezone_offset'];
	} else {
		$selected = drizzo_global_options('timezone_offset');
	}
	$zones = array(
		'-12'	=>	'UTC -12 ',
		'-11'	=>	'UTC -11',
		'-10'	=>	'UTC -10&nbsp;&nbsp;: Hawaii-Aleutian Time',
		'-9.5'	=>	'UTC -9.5',
		'-9'	=>	'UTC -9&nbsp;&nbsp;&nbsp;: Alaska Time',
		'-8'	=>	'UTC -8&nbsp;&nbsp;&nbsp;: Pacific Time',
		'-7'	=>	'UTC -7&nbsp;&nbsp;&nbsp;: Mountain Time',
		'-6'	=>	'UTC -6&nbsp;&nbsp;&nbsp;: Central Time',
		'-5'	=>	'UTC -5&nbsp;&nbsp;&nbsp;: Eastern Time',
		'-4'	=>	'UTC -4&nbsp;&nbsp;&nbsp;: Atlantic Time',
		'-3.5'	=>	'UTC -3.5&nbsp;: Newfoundland Time',
		'-3'	=>	'UTC -3',
		'-2'	=>	'UTC -2',
		'-1'	=>	'UTC -1',
		'0'		=>	'UTC&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;: Greenwich Mean Time, Western European Time',
		'+1'	=>	'UTC +1&nbsp;&nbsp;&nbsp;: Central European Time, West Africa Time',
		'+2'	=>	'UTC +2&nbsp;&nbsp;&nbsp;: Eastern European Time, Egypt Standard Time, Central Africa Time, Israel Standard Time, South Africa Standard Time',
		'+3'	=>	'UTC +3&nbsp;&nbsp;&nbsp;: Moscow Time, East Africa Time',
		'+3.5'	=>	'UTC +3.5&nbsp;: Tehran Time',
		'+4'	=>	'UTC +4',
		'+4.5'	=>	'UTC +4.5',
		'+5'	=>	'UTC +5&nbsp;&nbsp;&nbsp;: Pakistan Standard Time, West Asia Standard Time, Yekaterinburg Time',
		'+5.75'	=>	'UTC +5.75',
		'+6'	=>	'UTC +6&nbsp;&nbsp;&nbsp;: Bhutan Time, Bangladesh Time',
		'+6.5'	=>	'UTC +6.5',
		'+7'	=>	'UTC +7',
		'+8'	=>	'UTC +8&nbsp;&nbsp;&nbsp;: ASEAN Common Time',
		'+8.75'	=>	'UTC +8.75',
		'+9'	=>	'UTC +9',
		'+9.5'	=>	'UTC +9.5&nbsp;: Australian Central Time',
		'+10'	=>	'UTC +10&nbsp;&nbsp;: Australian Eastern Time, Chamorro Time',
		'+11'	=>	'UTC +11',
		'+12'	=>	'UTC +12&nbsp;&nbsp;: New Zealand Time',
		'+12.75'=>	'UTC +12.75&nbsp;: Chatham Islands Time',
		'+13'	=>	'UTC +13',
		'+14'	=>	'UTC +14'
		);
	$dropdown = "<select name=\"timezone_offset\" id=\"timezones_dropdown\">\n";
	foreach($zones as $utc => $zone) {
		if($utc == $selected) {
			$dropdown .= "\t<option selected value=\"$utc\">$zone</option>\n";
		} else {
			$dropdown .= "\t<option value=\"$utc\">$zone</option>\n";
		}
	}
	$dropdown .= "</select>";
	return $dropdown;
}

function drizzo_events_prev_and_next($previous_text = "&#171;", $next_text = "&#187;", $between_text = null, $previous_before = '<span class="previous_link">', $previous_after = '</span>', $next_before = '<span class="next_link">', $next_after = '</span>') {
	global $drizzo_events, $drizzo_event_year, $drizzo_event_month, $drizzo_event_day, $drizzo_post_id;
	if(isset($drizzo_event_year)) {
		$year	=	$drizzo_event_year;
	} else {
		$year	=	gmdate('Y',gmmktime(gmdate('H') + drizzo_global_options('timezone_offset') + date('I'),gmdate('i'),gmdate('s'),gmdate('m'),gmdate('d'),gmdate('Y')));
	}
	if(isset($drizzo_event_month)) {
		$month	=	$drizzo_event_month;
	} else {
		$month	=	gmdate('m',gmmktime(gmdate('H') + drizzo_global_options('timezone_offset') + date('I'),gmdate('i'),gmdate('s'),gmdate('m'),gmdate('d'),gmdate('Y')));
	}
	if(isset($drizzo_event_day)) {
		$day	=	$drizzo_event_day;
	} else {
		$day	=	gmdate('d',gmmktime(gmdate('H') + drizzo_global_options('timezone_offset') + date('I'),gmdate('i'),gmdate('s'),gmdate('m'),gmdate('d'),gmdate('Y')));
	}
	$id		=	$drizzo_post_id;
	$links	=	null;
	if($drizzo_post_id > 0) { // Links for individual event
		$this_event = drizzo_get_event($id);
		$prev_event_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_events` WHERE `start`<='$this_event[start_raw]' AND `ID`!='$id' ORDER BY `start` DESC LIMIT 1");
		if(!mysql_errno()) {
			$prev_event = drizzo_get_event(@mysql_result($prev_event_lookup,0));
		}
		$next_event_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_events` WHERE `start`>='$this_event[start_raw]' AND `ID`!='$id' ORDER BY `start` ASC LIMIT 1");
		if(!mysql_errno()) {
			$next_event = drizzo_get_event(@mysql_result($next_event_lookup,0));
		}
		if($prev_event) {
			$links .= "$previous_before<a href=\"" . drizzo_event_permalink($prev_event['ID']) . "\" title=\"$prev_event[what]\">$previous_text $prev_event[what]</a>$previous_after";
		}
		if($prev_next && $next_event) {
			$links .= $between_text;
		}
		if($next_event) {
			$links .= "$next_before<a href=\"" . drizzo_event_permalink($next_event['ID']) . "\" title=\"$next_event[what]\">$next_text $next_event[what]</a>$next_after";
		}
	} elseif($drizzo_event_day > 0) { // Links for an individual day
		$prev_event_lookup = @mysql_query("SELECT `start` FROM `$drizzo_events` WHERE `start`<'" . gmmktime(0,0,0,$month,$day,$year) . "' ORDER BY `start` DESC LIMIT 1");
		if(!mysql_errno()) {
			$prev_event = @mysql_result($prev_event_lookup,0);
		}
		$next_event_lookup = @mysql_query("SELECT `start` FROM `$drizzo_events` WHERE `start`>'" . gmmktime(23,59,59,$month,$day,$year) . "' ORDER BY `start` ASC LIMIT 1");
		if(!mysql_errno()) {
			$next_event = @mysql_result($next_event_lookup,0);
		}
		if($prev_event) {
			$links .= "$previous_before<a href=\"" . domain .  drizzo_global_options('events_basename') . "/" . gmdate('Y/m/d', drizzo_timezone_offset($prev_event)) . "\" title=\"" . gmdate('M j, Y', drizzo_timezone_offset($prev_event)) . "\">" . gmdate('M j, Y', drizzo_timezone_offset($prev_event)) . " $previous_text</a>$previous_after";
		}
		if($prev_next && $next_event) {
			$links .= $between_text;
		}
		if($next_event) {
			$links .= "$next_before<a href=\"" . domain .  drizzo_global_options('events_basename') . "/" . gmdate('Y/m/d', drizzo_timezone_offset($next_event)) . "\" title=\"" . gmdate('M j, Y', drizzo_timezone_offset($next_event)) . "\">$next_text " . gmdate('M j, Y', drizzo_timezone_offset($next_event)) . "</a>$next_after";
		}
	} elseif($drizzo_event_month > 0) { // Links for an individual month
		if($month - 1 == 0) {
			$new_month = 12;
			$new_year = $year - 1;
		} else {
			$new_month = $month - 1;
			$new_year = $year;
		}
		$title = gmdate('M Y', gmmktime(0,0,0,$new_month,1,$new_year));
		$links .= "$previous_before<a href=\"" . domain . drizzo_global_options('events_basename') . "/$new_year/$new_month/\" title=\"$title\">$previous_text $title</a>$previous_after";
		if($month + 1 == 13) {
			$new_month = 1;
			$new_year = $year + 1;
		} else {
			$new_month = $month + 1;
			$new_year = $year;
		}
		$title = gmdate('M Y', gmmktime(0,0,0,$new_month,1,$new_year));
		$links .= "$next_before<a href=\"" . domain .  drizzo_global_options('events_basename') . "/$new_year/$new_month/\" title=\"$title\">$title $next_text</a>$next_after";
	} elseif($year > 0) { // Links for an individual year
		$links .= "$previous_before<a href=\"" . domain .  drizzo_global_options('events_basename') . "/" . ($year - 1) . "/\" title=\"" . ($year - 1) . "\">$previous_text " . ($year - 1) . "</a>$previous_after";
		$links .= "$next_before<a href=\"" . domain .  drizzo_global_options('events_basename') . "/" . ($year + 1) . "/\" title=\"" . ($year + 1) . "\">$next_text " . ($year + 1) . "</a>$next_after";
	}
	return $links;
}

function drizzo_event_tags($id = null, $style = "comma", $links = "true") {
	global $drizzo_events, $the_event;
	
	drizzo_db_connect();
	
	if($id == null && empty($the_event)) {
		return false;
	} else {
		if($id > 0) {
			$the_event = drizzo_get_the_event($id);
		}

		$tags = explode(", ", $the_event['tags']);
		$tags_lower = array_map('strtolower', $tags);
		array_multisort($tags_lower, SORT_ASC, SORT_STRING, $tags);

		if($style == "comma") {
				$tag_count = 1;
				$tags_string = null;
				foreach($tags as $tag) {
					$tag_nice = str_replace("-", " ", $tag);
					if($links == "true") {
						if($tag_count < count($tags)) {
							$tags_string .= "<a href=\"/tags/" . $tag . "\">" . $tag_nice . "</a>, ";
						} else {
							$tags_string .= "<a href=\"/tags/" . $tag . "\">" . $tag_nice . "</a>";
						}
					} else {
						if($tag_count < count($tags)) {
							$tags_string .= $tag_nice . ", ";
						} else {
							$tags_string .= $tag_nice;
						}
					}
					$tag_count++;
				}

			return $tags_string;
		}
		
		if($style == ("list" || "ul" || "ol")) {
			if($style == ("list" || "ul")) { $list_type = "ul"; }
			if($style ==("ol")) { $list_type = "ol"; }
			
			$tags_query = @mysql_query("SELECT `tags` FROM `$drizzo_events` WHERE `ID`='$id'");
			if(!mysql_errno()) {
				while($row = @mysql_fetch_assoc($tags_query)) {
					$tags = $row['tags'];
				}
				$tag = explode(", ", $tags);
				$tag_lower = array_map('strtolower', $tag);
				array_multisort($tag_lower, SORT_ASC, SORT_STRING, $tag);
				$tag_count = count($tag);
				$tags_string .= "<" . $list_type . " id=\"tags-list\">\n";
					for($no = 0; $no < $tag_count; $no++) {
						if($links == "true") {
							$tag_nice[$no] = preg_replace("/-/", " ", $tag[$no]);
							$tags_string .= "\t<li class=\"tag\"><a href=\"/tags/" . $tag[$no] . "\">" . $tag_nice[$no] . "</a></li>\n";
						} else {
							$tag_nice[$no] = preg_replace("/-/", " ", $tag[$no]);
							$tags_string .= "\t<li class=\"tag\">" . $tag[$no] . "</li>\n";
						}
					}
				$tags_string .= "</" . $list_type . ">\n";

				return $tags_string;
			} else {
				return false;
			}
		}
	}
}

function drizzo_get_event_id($event_permalink) {
	drizzo_db_connect();
	global $drizzo_events;
	$event_id_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_events` WHERE `permalink_name`='$event_permalink'");
	if(!mysql_errno()) {
		$event_id = @mysql_result($event_id_lookup,0);
	} else {
		$event_id = false;
	}
	return $event_id;
}

function drizzo_events() {
	global $drizzo_event_month,$drizzo_event_day,$drizzo_event_year,$drizzo_post_id;
	if(is_numeric($drizzo_post_id)) { // If an individual event has been specified
		$the_events[] = drizzo_get_event($drizzo_post_id);
	} elseif(drizzo_global_options('events_default') == 'table' && !$drizzo_event_day) { // If the default view is set to Calendar Month and no day has been specified
		if($drizzo_event_month > 0) { // If a month has been specified
			$the_events = drizzo_events_month('detailed',$drizzo_event_month,$drizzo_event_year);
		} elseif($drizzo_event_year > 0) { // If a year has been specified
			$the_events = null;
			for($i = 1; $i < 13; $i++) { // Loop through the 12 months
				$the_events .= drizzo_events_month('detailed',$i,$drizzo_event_year);
			}
		} else { // If the url is simply /events_basename
			$the_events = drizzo_events_month('detailed',gmdate('m',gmmktime(0 - drizzo_global_options('timezone_offset') - date('I'),0,0,gmdate('m'),gmdate('d'),gmdate('Y'))),gmdate('Y',gmmktime(0 - drizzo_global_options('timezone_offset') - date('I'),0,0,gmdate('m'),gmdate('d'),gmdate('Y'))));
		}
	} else { // If the default view is set to list or if a day has been specified
		if($drizzo_event_day > 0) { // If a day has been specified
			$the_events = drizzo_get_events(gmmktime(0 - drizzo_global_options('timezone_offset') - date('I'),0,0,$drizzo_event_month,$drizzo_event_day,$drizzo_event_year),gmmktime(23 - drizzo_global_options('timezone_offset') - date('I'),59,60,$drizzo_event_month,$drizzo_event_day,$drizzo_event_year));
		} elseif($drizzo_event_month > 0) { // If a month has been specified
			$the_events = drizzo_get_events(gmmktime(0 - drizzo_global_options('timezone_offset') - date('I'),0,0,$drizzo_event_month,1,$drizzo_event_year),gmmktime(23 + drizzo_global_options('timezone_offset') - date('I'),59,60,$drizzo_event_month,gmdate('t',gmmktime(0 + drizzo_global_options('timezone_offset') - date('I'),0,0,$drizzo_event_month,1,$drizzo_event_year)),$drizzo_event_year));
		} elseif($drizzo_event_year > 0) { // If a year has been specified
			$the_events = drizzo_get_events(gmmktime(0 - drizzo_global_options('timezone_offset') - date('I'),0,0,1,1,$drizzo_event_year),gmmktime(23 - drizzo_global_options('timezone_offset') - date('I'),59,60,12,31,$drizzo_event_year));
		} else { // If the url is simply /events_basename
			$the_events = drizzo_get_events(gmmktime(0 - drizzo_global_options('timezone_offset') - date('I'),0,0,gmdate('m'),gmdate('d'),gmdate('Y')));
		}
	}
	return $the_events;
}
?>
